package com.hnluchuan.bmjlearning.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.hnluchuan.bmjlearning.common.DeleteStatus;
import com.hnluchuan.bmjlearning.common.ServicePackageStatus;
import com.hnluchuan.bmjlearning.common.ServicePackageType;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;
	
import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.bmjlearning.model.ServicePackage;
import com.hnluchuan.bmjlearning.dto.ServicePackageDTO;
import com.hnluchuan.core.support.Page;

@Repository
public class ServicePackageDAO extends BaseDAO<ServicePackage> {

    public List<ServicePackage> find(ServicePackageDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From ServicePackage n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getStatus() != null) {
            hql.append(" and n.status = ? ");
            paras.add(dto.getStatus());
        }
        if (dto.getType() != null) {
            hql.append(" and n.type = ? ");
            paras.add(dto.getType());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            hql.append(" and n.name like ? ");
            paras.add("%" + dto.getName() + "%");
        }					
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public ServicePackage findOne(ServicePackageDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<ServicePackage> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public int enableOrDisableByIds(List<Long> ids, Integer status){
        String hql = "Update ServicePackage sp set sp.status = ? where sp.id in (:list)";
        List<Object> paras = new ArrayList<>();
        paras.add(status);
        paras.add(ids);

        return super.executeUpdate(hql, paras.toArray());
    }

    public List<ServicePackage> findByAgentId(Long agentId){
        String sql = "Select distinct * from t_service_package sp inner join t_agent_service_package asp " +
                                                        "on sp.id = asp.service_package_id " +
                                                        "inner join t_agent a " +
                                                        "on a.id = asp.agent_id " +
                                                        "where sp.status = ? and asp.deleted = ? and a.id = ?";
        List<Object> paras = new ArrayList<>();
        paras.add(ServicePackageStatus.Enabled.getValue());
        paras.add(DeleteStatus.NotDeleted.getValue());
        paras.add(agentId);

        return super.findModelBySql(sql, paras.toArray(), null);
    }

    public List<ServicePackage> findOfficialByAgentId(Long agentId){
        String sql = "Select distinct * from t_service_package sp inner join t_agent_service_package asp " +
                " on sp.id = asp.service_package_id " +
                " inner join t_agent a " +
                " on a.id = asp.agent_id " +
                " where sp.status = ? and asp.deleted = ? and a.id = ? and sp.type = ? ";
        List<Object> paras = new ArrayList<>();
        paras.add(ServicePackageStatus.Enabled.getValue());
        paras.add(DeleteStatus.NotDeleted.getValue());
        paras.add(agentId);
        paras.add(ServicePackageType.OFFICIAL.getValue());
        return super.findModelBySql(sql, paras.toArray(), null);
    }


    public List<ServicePackage> findHistoryByAgentId(Long agentId){
        String sql = "Select distinct * from t_service_package sp inner join t_agent_service_package asp " +
                "on sp.id = asp.service_package_id " +
                "inner join t_agent a " +
                "on a.id = asp.agent_id " +
                "where  a.id = ? group by sp.id;";
        List<Object> paras = new ArrayList<>();
        paras.add(agentId);
        return super.findModelBySql(sql, paras.toArray(), null);
    }
    
    @Override
	public Long create(ServicePackage servicePackage) {
	    if (servicePackage.getCreated() == null) {    
	        servicePackage.setCreated(new Date());
	    }
        if (servicePackage.getType() == null) {
            servicePackage.setType(ServicePackageType.OFFICIAL.getValue());
        }
        return super.create(servicePackage);
	}

	@Override
	public void update(ServicePackage servicePackage) {
		super.update(servicePackage);
	}
    
}
